Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb] Correct format specifier for sscanf to prevent buffer overflow (NFC) #94783

Closed
wants to merge 3 commits into from

Conversation

xgupta
Copy link
Contributor

@xgupta xgupta commented Jun 7, 2024

The format specifier for the 'comm' field in the sscanf function was corrected to limit the input size and prevent potential buffer overflow. The '%s' specifier was replaced with '%15s' to ensure the 'comm' field does not exceed 15 characters.

Value 15 is chosen because 'comm' array have 16 length including ‘\0’ character at the end.
char comm[task_comm_len];
constexpr int task_comm_len = 16;

Caught by cppcheck -
lldb/source/Host/linux/Host.cpp:94:7: warning: sscanf() without field width limits can crash with huge input data. [invalidscanf]

Fix #89710

@xgupta xgupta requested a review from JDevlieghere as a code owner June 7, 2024 18:08
@llvmbot llvmbot added the lldb label Jun 7, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-lldb

Author: Shivam Gupta (xgupta)

Changes

Fix #89710


Full diff: https://github.com/llvm/llvm-project/pull/94783.diff

1 Files Affected:

  • (modified) lldb/source/Host/linux/Host.cpp (+1-1)
diff --git a/lldb/source/Host/linux/Host.cpp b/lldb/source/Host/linux/Host.cpp
index c6490f2fc9e2f..8a65c46a52ea8 100644
--- a/lldb/source/Host/linux/Host.cpp
+++ b/lldb/source/Host/linux/Host.cpp
@@ -92,7 +92,7 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
     return false;
   StatFields stat_fields;
   if (sscanf(Rest.data(),
-             "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld",
+             "%d %15s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld",
              &stat_fields.pid, stat_fields.comm, &stat_fields.state,
              &stat_fields.ppid, &stat_fields.pgrp, &stat_fields.session,
              &stat_fields.tty_nr, &stat_fields.tpgid, &stat_fields.flags,

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar concern as #94775 (comment)

@xgupta xgupta changed the title [LLDB][NFC] Fix a cppcheck warning in lldb/source/Host/linux/Host.cpp [lldb] Correct format specifier for sscanf to prevent buffer overflow (NFC) Jun 8, 2024
Copy link

github-actions bot commented Jun 15, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@DavidSpickett
Copy link
Collaborator

Please remove the formatting changes and limit this to just the specifier change.

I know most of the time we'd want it formatted but for a fix as limited as this, the formatting change just gets in the way, and not formatting it doesn't block merging either.

Otherwise this LGTM once that's done.

@DavidSpickett
Copy link
Collaborator

The bot will complain again but we will ignore it :)

Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field this is consuming is actually 17 bytes long, because the process name is in parenthesis. I suspect this will cause the function to reject any process whose name is longer than 13 characters.

The name field is actually quite hard to parse this way since it can contain any character (esp. parenthesis and spaces). Now, we could devise an algorithm to do that, but since the code is later opening /proc/$PID/status anyway, and status contains a superset of information, I think it'd be best to just delete this code and extract the information we want from there.

status parsing code also uses more modern and less error prone patterns.

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field this is consuming is actually 17 bytes long, because the process name is in parenthesis.

Ok then I am confused how this ever worked, but it sounds like scanf was never a great way to do this anyway?

@labath
Copy link
Collaborator

labath commented Jul 24, 2024

The field this is consuming is actually 17 bytes long, because the process name is in parenthesis.

Ok then I am confused how this ever worked, but it sounds like scanf was never a great way to do this anyway?

The field it's overwriting is in a struct, so it has a lot of headroom for "safely" overflowing without hitting anything important. And since the the other fields are parsed after the string field, they probably just immediately overwrite the corrupted data.

@DavidSpickett
Copy link
Collaborator

In that case, best not to store it at all at least until a better way to parse it is found - #100387.

@DavidSpickett
Copy link
Collaborator

I have merged #100387 to fix this instead, because it needed some background knowledge to do correctly.

But never the less, thank you @xgupta for making the effort to do this PR, it has uncovered some unsafe code that only recently went in. Which is a great result in itself!

@xgupta
Copy link
Contributor Author

xgupta commented Jul 25, 2024

Thanks, @DavidSpickett for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lldb/source/Host/linux/Host.cpp:94: Possible missing field width in scanf %s ?
5 participants